// pages/addStudent/addStudent.js
var api = require('../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // str:0,
    payType: 0,
    sexArr: ['男', '女'],
    arrIdx: '',
    sexIdx: '',
    projecIdx: '',
    classTypeIdx: '',
    campusIdx: '',
    stockIdx: '',
    stockIdx0: '',
    stockIdx1: '',
    stockIdx2: '',
    stockIdx3: '',
    specIdx: '',
    specIdx0: '',
    specIdx1: '',
    specIdx2: '',
    specIdx3: '',
    brithday: '',
    signTime: '',
    dueDate: '',
    classArr: [],
    projectArr: [], //校区
    campusArr: [], //项目
    classType: ['常年', '暑假', '寒假', '学期', '小课', '次卡', '集训'],
    stockArr: [],
    specArr: [],
    addArr: [],
    specs:[]
  },

  //获取校区、项目、班级
  getSchool() {
    api.fetchApi('reception/getsearch', {
      token: wx.getStorageSync('token')
    }).then(res => {
      var classArr = res.class;
      var projectArr = res.project;
      var campusArr = res.campus
      console.log(campusArr, classArr, projectArr)
      this.setData({
        classArr,
        projectArr,
        campusArr
      })

    })
  },

  //缴费方式
  selectType(e) {
    var payType = e.currentTarget.id;
    this.setData({
      payType
    })

  },
  //选择性别
  sexSelect(e) {
    var sexIdx = e.detail.value
    this.setData({
      sexIdx
    })
  },

  //生日选择
  selectBirth(e) {
    console.log(e.detail.value)
    this.setData({
      brithday: e.detail.value
    })
  },

  //报名时间选择
  selectSignTime(e) {
    this.setData({
      signTime: e.detail.value
    })
  },

  //到期日期
  selectDueDate(e) {
    this.setData({
      dueDate: e.detail.value
    })
  },



  //选择校区
  selectSchool(e) {
    var idx = e.detail.value;
    var nowArr = this.data.projectArr[idx];
    var campus_id = nowArr.id
    // console.log(campus_id)
    this.setData({
      campus_id,
      projecIdx: idx
    })


    //后台会返回库存
    api.fetchApi('reception/getstock', { token: wx.getStorageSync('token'), campus: campus_id }).then(res => {
      this.setData({
        stockArr: res,
        stockIdx: '',
        specIdx: '',
        specArr: []
      })
    })
  },

  //选择库存
  selectStock(e) {
    var idx = e.detail.value;
    var nowArr = this.data.stockArr[idx]
    console.log(nowArr)
    this.setData({
      stockIdx: idx,
      stockId: nowArr.id
    })

    //后台会返回规格
    api.fetchApi('reception/getspec', { token: wx.getStorageSync('token'), id: nowArr.id }).then(res => {
      this.setData({
        specArr: res,
        specIdx: ''
      })
    })
  },


  //选择规格
  selectSpec(e) {
    var idx = e.detail.value;
    var nowArr = this.data.specArr[idx];
    this.setData({
      specIdx: idx,
      specId: nowArr.id,
    })
  },

  
  //送第二个
  selectStock0(e){
    var idx = e.detail.value;
    var nowArr = this.data.stockArr[idx]
    console.log(nowArr)
    this.setData({
      stockIdx0: idx,
      stockId0: nowArr.id
    })

    //后台会返回规格
    api.fetchApi('reception/getspec', { token: wx.getStorageSync('token'), id: nowArr.id }).then(res => {
      this.setData({
        specArr0: res,
        specIdx0: ''
      })
    })
  },

  selectSpec0(e) {
    var idx = e.detail.value;
    var nowArr = this.data.specArr0[idx];
    this.setData({
      specIdx0: idx,
      specId0: nowArr.id,
    })
  },

  //送第三个
  selectStock1(e) {
    var idx = e.detail.value;
    var nowArr = this.data.stockArr[idx]
    console.log(nowArr)
    this.setData({
      stockIdx1: idx,
       stockId1: nowArr.id
    })

    //后台会返回规格
    api.fetchApi('reception/getspec', { token: wx.getStorageSync('token'), id: nowArr.id }).then(res => {
      this.setData({
        specArr1: res,
        specIdx1: ''
      })
    })
  },

  selectSpec1(e) {
    var idx = e.detail.value;
    var nowArr = this.data.specArr1[idx];
    this.setData({
      specIdx1: idx,
       specId1: nowArr.id,
    })
  },

  //送第四个
  selectStock2(e) {
    var idx = e.detail.value;
    var nowArr = this.data.stockArr[idx]
    console.log(nowArr)
    this.setData({
      stockIdx2: idx,
       stockId2: nowArr.id
    })

    //后台会返回规格
    api.fetchApi('reception/getspec', { token: wx.getStorageSync('token'), id: nowArr.id }).then(res => {
      this.setData({
        specArr2: res,
        specIdx2: ''
      })
    })
  },

  selectSpec2(e) {
    var idx = e.detail.value;
    var nowArr = this.data.specArr2[idx];
    this.setData({
      specIdx2: idx,
       specId2: nowArr.id,
    })
  },

  //送第五个
  selectStock3(e) {
    var idx = e.detail.value;
    var nowArr = this.data.stockArr[idx]
    console.log(nowArr)
    this.setData({
      stockIdx3: idx,
       stockId3: nowArr.id
    })

    //后台会返回规格
    api.fetchApi('reception/getspec', { token: wx.getStorageSync('token'), id: nowArr.id }).then(res => {
      this.setData({
        specArr3: res,
        specIdx3: ''
      })
    })
  },

  selectSpec3(e) {
    var idx = e.detail.value;
    var nowArr = this.data.specArr3[idx];
    this.setData({
      specIdx3: idx,
       specId3: nowArr.id,
    })
  },



  




  //选择报班类型
  selectClassType(e) {
    var idx = e.detail.value;
    this.setData({
      classTypeIdx: idx,
      class_type: idx
    })
  },

  //选择项目
  selectProject(e) {
    var idx = e.detail.value;
    var nowArr = this.data.projectArr[idx];
    var project = nowArr.id
    this.setData({
      campusIdx: idx,
      project
    })
  },

  //表单提交
  formSubmit(e) {
    console.log(e.detail.value);
    if (!this.data.stockId) { this.data.stockId=''};
    if (!this.data.specId) { this.data.specId = '' };
    if (!this.data.stockId0) { this.data.stockId0 = '' };
    if (!this.data.specId0) { this.data.specId0 = '' };
    if (!this.data.stockId1) { this.data.stockId1 = '' };
    if (!this.data.specId1) { this.data.specId1 = '' };
    if (!this.data.stockId2) { this.data.stockId2 = '' };
    if (!this.data.specId2) { this.data.specId2 = '' };
    if (!this.data.stockId3) { this.data.stockId3 = '' };
    if (!this.data.specId3) { this.data.specId3 = '' };
    var spec = "" + this.data.stockId + ":" + "" + this.data.specId + "," + "" + this.data.stockId0 + ":" + "" + this.data.specId0 + "," + "" + this.data.stockId1 + ":" + "" + this.data.specId1 + "," + "" + this.data.stockId2 + ":" + "" + this.data.specId2 + "," + "" + this.data.stockId3 + ":" + "" + this.data.specId3
    var formValue = e.detail.value
    var params = {
      token: wx.getStorageSync('token'),
      phone: formValue.phone,
      name: formValue.name,
      sex: this.data.sexIdx,
      brithday: this.data.brithday,
      remark: formValue.remark,
      registration_date: this.data.signTime,
      due_date: this.data.dueDate,
      campus: this.data.campus_id,
      project: this.data.project,
      tuition: formValue.tuition,
      school_hour: formValue.school_hour,
      pay_type: this.data.payType,
      class_type: this.data.class_type,
      // stock: this.data.stockId || 0,
      spec
    }

    if (this.data.stockIdx !== '' && this.data.specIdx === '') {
      api.showToast('请填写规格', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }

    if (!formValue.name) {
      api.showToast('请填写学生姓名', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }

    if (!this.data.sexIdx) {
      api.showToast('请填写性别', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    if (!formValue.phone) {
      api.showToast('请填写联系方式', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    if (!this.data.brithday) {
      api.showToast('请填写生日', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    if (!this.data.campus_id) {
      api.showToast('请填写校区', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    if (!this.data.project) {
      api.showToast('请填写项目', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    if (!this.data.class_type) {
      api.showToast('请填写报班类型', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    if (!this.data.signTime || !this.data.dueDate) {
      api.showToast('请填写报班时间', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }
    if (!formValue.tuition) {
      api.showToast('请填写续费金额', 'http://qnwmfx.laigl.com/iconimg/warn.png')
      return
    }

    api.fetchApi('reception/add_student', params).then(res => {


      api.showToast('添加成功').then(() => {


        setTimeout(() => {
          wx.navigateBack({

          })
        }, 1500)


      })


    })
  },

  //加库存
  addStock(e) {
    // console.log(e.currentTarget.dataset.id)
    var addArr = this.data.addArr;
    addArr.push(1);
    if(addArr.length>=4){
      addArr = addArr.slice(0,4)
      console.log(addArr)
    }
    this.setData({
      addArr
    })
  },

  //减库存
  reduceStock() {
    var addArr = this.data.addArr;
    addArr = addArr.slice(0, -1);
    console.log(addArr)
    this.setData({
      addArr
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSchool()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})